package service;

import com.mysql.cj.jdbc.Driver;
import com.mysql.cj.x.protobuf.MysqlxDatatypes;
import data.Student;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DbConnect<T> {
    private static String url="jdbc:mysql://127.0.0.1:3306/exam";
    private static String user="root";
    private static String pwd="212778";
    private Connection con;

    public PreparedStatement preparedStatement(String sql){
        try{
            con();
            return con.prepareStatement(sql);
        }catch (Exception e){
            System.out.println("错误"+e.getMessage());
            return null;
        }
    }

    public void que(PreparedStatement p) throws SQLException {
        p.executeUpdate();
        con.close();
    }
    public Statement con(){
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
           con = DriverManager.getConnection(url, user, pwd);
           return con.createStatement();
        }catch (Exception e){
            System.out.println("错误："+e.getMessage());
        }
        return null;
    }

    public List<T> selectList(String sql,T t) throws Exception {
        Class<?> aClass = t.getClass();
        Field[] declaredFields = aClass.getDeclaredFields();
        Statement statement = con();
        ResultSet resultSet = statement.executeQuery(sql);
        List<T> data=new ArrayList<>();
        while (resultSet.next()){
            T obj= (T) aClass.newInstance();
            for (Field declaredField : declaredFields) {
                Object value="";
                String fname=declaredField.getName();
                String type=declaredField.getType().getSimpleName();
                   if(type.equalsIgnoreCase("string")){
                                         value=resultSet.getString(fname);
                       }else if(type.equalsIgnoreCase("int")){
                                         value=resultSet.getInt(fname);
                      }else if(type.equalsIgnoreCase("Integer")){
                                         value=resultSet.getInt(fname);
                       }else if(type.equalsIgnoreCase("Double")){
                                         value=resultSet.getDouble(fname);
                       }else if(type.equalsIgnoreCase("Float")){
                                         value=resultSet.getFloat(fname);
                       }else if(type.equalsIgnoreCase("date")){
                                         value=resultSet.getDate(fname);
                       }else if(type.equalsIgnoreCase("long")){
                                         value=resultSet.getLong(fname);
                       }
                System.out.println(value);
                Method[] declaredMethods = aClass.getDeclaredMethods();
                for (Method declaredMethod : declaredMethods) {
                    if(declaredMethod.getName().equalsIgnoreCase("set"+declaredField.getName()))
                        declaredMethod.invoke(obj,value);
                }
            }
            data.add(obj);
        }
        return data;
    }

    public T selectOne(String sql,T t) throws Exception{
//        查询某一个数据
        List<T> ts = selectList(sql, t);

        return ts.size()==0?null: ts.get(0);

    }

    public boolean execute(String sql) throws Exception{
//        增删改的内容
        Statement con1 = con();
        boolean execute = con1.execute(sql);
        con.close();
        return execute;
    }


}
